﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace Medianamik.UI.Web.MVP.View
{
    public abstract class ViewWithGridAndDefaultFilters<T> : ViewWithGrid<T>, 
        IViewWithGridAndDefaultFilters
        where T : IViewWithGrid
    {
        public abstract DateTime? SelectedStartDateTime { get; set; }
        public abstract DateTime? SelectedEndDateTime { get; set; }
        public abstract IEnumerable<ListItem> Status { get; set; }
        public abstract string Keywords { get; set; }
        public event EventHandler Filter;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            Grid.ActivationStatusChanged += GridActivationStatusChanged;
            Grid.NodeDeleted += GridNodeDeleted;
            Grid.PageIndexChanged += GridPageIndexChanged;
            Grid.LockRequestFailed += GridLockRequestFailed;
        }

        protected virtual void OnFilter()
        {
            var handler = Filter;
            if (handler != null)
            {
                handler(this, null);
            }
        }

        void GridLockRequestFailed(object sender, IdEventArgs e)
        {
            OnLockRequestFailed(e);
        }

        protected virtual void GridPageIndexChanged(object sender, GridPageChangedEventArgs e)
        {
            OnPageIndexChanged(new PageIndexChangedEventArgs(e.NewPageIndex, false));
        }

        void GridNodeDeleted(object sender, EventArgs e)
        {
            OnNodeDeleted();
        }

        void GridActivationStatusChanged(object sender, ActivationStatusChangedEventArgs e)
        {
            OnActivationStatusChanged(e);
        }
    }
}